package com.oocl.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.pojo.Dish;
import com.oocl.pojo.DishType;
import com.oocl.pojo.Merchant;
import com.oocl.service.DishManager;
import com.oocl.service.DishTypeManager;
import com.oocl.service.factory.DishManagerFactory;
import com.oocl.service.factory.DishTypeManagerFactory;
import com.oocl.util.ValidateUtil;

@WebServlet("/sec/dishes/list")
public class ListDishesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private DishManager dishManager = DishManagerFactory.getInstance();
	private DishTypeManager dishTypeManager = DishTypeManagerFactory.getInstance();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// get search conditions
		String _dType = request.getParameter("dtype");
		String _dName = request.getParameter("dname");
		
		Integer dType = 0;
		String dName = null;
		
		if (!ValidateUtil.isNullOrEmpty(_dType)) {
			dType = Integer.parseInt(_dType);
		}
		if (!ValidateUtil.isNullOrEmpty(_dName)) {
			dName = _dName;
		}
		
		Merchant m = (Merchant) request.getSession().getAttribute("merchant");
		
		// TODO session
		List<Dish> dishes = dishManager.findDishes(dType, dName, m.getmId());
		request.setAttribute("dishes", dishes);
		
		List<DishType> dishTypes = dishTypeManager.getAllDishTypes();
		request.setAttribute("dishTypes", dishTypes);
		
		request.getRequestDispatcher("/WEB-INF/views/list-dish.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
